<?php require_once(dirname(__FILE__)."/../config/config.php"); ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>礼品配送管理页面</title>
<style type="text/css">
a:link {
text-decoration: none;
}
a:visited {
text-decoration: none;
}
a:hover {
text-decoration: none;
}
a:active {
text-decoration: none;
}
a.current {text-decoration: underline;font:bold}
</style> 
</head>
<body>
<?php  
if($_SESSION['admin_name']=="")
{

	echo "<script>location.href='admin_login.php';</script>";
}
$method = $_REQUEST['method'];
if($method == 'updateStatus')
{
	$id = $_REQUEST['id'];
	$page = $_REQUEST['page'];
	if($id > 0)
	{
		$querySql="select gift_status from zy_gift_delivery where id='$id'";
		$rs = mysql_query($querySql);
		$result = mysql_fetch_array($rs);
		if($result['gift_status'] == 0)
		{
			$sql="update zy_gift_delivery set `gift_status`='1',`Deliverytime`=now() where id='$id'";
			$rs = mysql_query($sql);
			if($rs > 0)
			{
					header("location:gift_delivery.php?page=".$page);
			}
		}
		else {
			header("location:gift_delivery.php");
		}
	}
}
?>
<center>
<h2>礼品管理系统</h2>
<table width="843" border="1" align="center">
 <tr>
   
  
	<th width="95">礼品名称</th>  
	<th width="95">用户姓名</th>
	<th width="142">礼品状态</th>
	<th width="142">兑换时间</th>
	<th width="200">配送时间</th>
	<th width="155">操作</th>
	
	
</tr>

 <?php 
 //========================
 // 1 定义分页变量
 	$page = isset($_GET["page"])?$_GET["page"]:1;					//当前页号
	$pageSize = 20;				//页面大小	
	$maxRows;					// 最大数据条
	$maxPages;					// 最大页数
 	
 
 // 2获取最大数据条数
 	$sql = "select count(*) from zy_gift_delivery";
	$res = mysql_query($sql);
	$maxRows = mysql_result($res,0,0);//定位获取总条数
 
 
 // 3计算出共计最大页数
 	$maxPages=ceil($maxRows/$pageSize);
	$init = 1;
	$max = $maxPages;
	$pagelen = 10;
	// $pagelen = ($pagelen%2)?$pagelen:$pagelen+1;
	$pageoffset = 4;
	if($maxPages>$pagelen){
		if($page<=$pageoffset)
		{
			// echo "~~~~~~~~";
			$init=1;
			$max = $pagelen;
		}else{
			if($page+$pageoffset>=$maxPages+1){
				$init = $maxPages-$pagelen+1;
				// echo "!!!!!!!!!!";
			}else{
				// echo "????????????";
				$init = $page-$pageoffset;
				$max=$page+$pageoffset+1;
			}	
		}
	
	}
	
	
 
 // 4 效验当前页数
 	if($page>$maxPages){
		$page=$maxPages;
	}
	if($page<1){
		$page=1;
	}
 
 
 
 //5 拼装分页sql语句片段
	$limit = " limit ".(($page-1)*$pageSize).",{$pageSize}"; 
 
 
 
 
 //==========================
 $sql = "SELECT * FROM zy_gift_delivery order by Conversiontime desc   {$limit}";
 // echo $sql;
 $result = mysql_query($sql);
 
 while($zy_gift_delivery = mysql_fetch_array($result)){
 ?>


  <tr>
   
   
	<td><?php $sql3="select * from zy_gift where gift_id='".$zy_gift_delivery['gift_id']."'";$result3=mysql_query($sql3);while($zy_gift=mysql_fetch_array($result3)){echo $zy_gift[gift_name];}?></td>
	<td><?php  
   $sql2 = "select * from zy_user where user_id = '".$zy_gift_delivery['user_id']."'";
   $result2 = mysql_query($sql2);
   while($zy_user=mysql_fetch_array($result2))
     {
 echo $zy_user['username'];}?></td>
	<td><?php if($zy_gift_delivery['gift_status']==0){
 			echo "未发货";
	}elseif ($zy_gift_delivery['gift_status']==1){
		echo"已发货";
	}
	?></td>
	<td><?php echo $zy_gift_delivery['Conversiontime']?></td>
	<td><?php echo $zy_gift_delivery['Deliverytime']?></td>
	<!-- <td align="center"><a href="gift_delivery_action.php?action=del&id=<?php echo $zy_gift_delivery['id'];?>">删除</a></td>
   -->
    <td align="center"><a href="gift_delivery.php?method=updateStatus&page=<?php echo $page?>&id=<?php echo $zy_gift_delivery['id'];?>">顾客状态改变</a></td>
	</tr>
  <?php } ?>
</table>
 <?php
	//输出分页信息显示上一页和下一页
	echo "<br/><br/>";
	if($page!=1&&$page!=$maxPages){
	echo "<a href='gift_delivery.php?page=1'>首页</a> ";
	echo "<a href='gift_delivery.php?page=".($page-1)."'>上一页</a> ";
	for($i=$init;$i<=$max;$i++){
		echo"<a href='gift_delivery.php?page=".($page=$i)."'> $i</a> ";
	}
	echo "<a href='gift_delivery.php?page=".($page+1)."'>下一页</a> ";
	echo "<a href='gift_delivery.php?page={$maxPages}'>末页</a> ";
	}elseif($page==1){
		for($i=$init;$i<=$max;$i++){
		echo"<a href='gift_delivery.php?page=".($page=$i)."'> $i</a> ";
	}
	echo "<a href='gift_delivery.php?page=".($page+1)."'>下一页</a> ";
	echo "<a href='gift_delivery.php?page={$maxPages}'>末页</a> ";
	}elseif($page==$maxPages){
	echo "<a href='gift_delivery.php?page=1'>首页</a> ";
	echo "<a href='gift_delivery.php?page=".($page-1)."'>上一页</a> ";
	for($i=$init;$i<=$max;$i++){
		echo"<a href='gift_delivery.php?page=".($page=$i)."'> $i</a> ";
	}
	}
?>
</center>
<h3 align="center"><a href="admin_management.php">返回</a></h3>
</body>
</html>
